//
//  SignatureViewController.m
//  WorkManager
//
//  Created by iD Student on 7/18/12.
//  Copyright (c) 2012 iD Student. All rights reserved.
//

#import "SignatureViewController.h"

@interface SignatureViewController ()

@end

@implementation SignatureViewController

@synthesize appointmentReport;
@synthesize clearButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
	// Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    CGRect frame;
    frame.origin.x=20;
    frame.origin.y=20;
    frame.size.width=280;
    frame.size.height=332;
    if(self.appointmentReport.signature==nil){
        signatureView = [[[SignatureView alloc] initWithFrame:frame] retain];
        [self.view addSubview:signatureView];
    }
    else {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        [imageView setImage:self.appointmentReport.signature];
        [self.view addSubview:imageView];
        [imageView release];
        imageView = nil;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if(self.appointmentReport.signature==nil){
        UIGraphicsBeginImageContext(signatureView.bounds.size);
        [signatureView.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        [self.appointmentReport setSignature:viewImage];
        UIGraphicsEndImageContext();
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [clearButton release];
    [appointmentReport release];
    [super dealloc];
}
- (IBAction)clear:(id)sender {
    if(self.appointmentReport.signature==nil){
        [signatureView removeFromSuperview];
        [signatureView release];
        signatureView = nil;
        CGRect frame;
        frame.origin.x=20;
        frame.origin.y=20;
        frame.size.width=280;
        frame.size.height=332;
        signatureView = [[SignatureView alloc] initWithFrame:frame];
        [self.view addSubview:signatureView];
        [signatureView release];
    }
    else {
        [self.appointmentReport setSignature:nil];
        [self clear:nil];
    }
}
@end
